package com.guigu.liuzhireng.controller;

import com.guigu.aop.OpLog;
import com.guigu.liuzhireng.pojo.Account;
import com.guigu.liuzhireng.pojo.Accountrole;
import com.guigu.liuzhireng.pojo.PageVo;
import com.guigu.liuzhireng.service.AccountService;
import com.guigu.liuzhireng.service.AccountroleService;
import com.guigu.liuzhireng.utils.PinyinUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("Account.do")
public class AccountController {

    @Autowired
    AccountService accountService;
    @Autowired
    AccountroleService accountroleService;

    @RequestMapping("LoadAccount")
    @ResponseBody
    public Account LoadAcc(HttpSession session) {
        return (Account) session.getAttribute("user");
    }

    @RequestMapping("AccountPwd")
    @ResponseBody

    public int AccountPwd(Account account, HttpSession session) throws IOException {
        String acc = ((Account) session.getAttribute("user")).getAccount();
        account.setAccount(acc);
        int i = accountService.update(account);

        session.removeAttribute("user");
        Account account1 = accountService.loadAccount(acc);
        session.setAttribute("user", account1);
        return i;
    }

    @RequestMapping("UpDateAccount")
    public String UpDateAccount(Account account, HttpServletRequest req, MultipartFile filename, HttpSession session) throws IOException {
        String realPath = req.getServletContext().getRealPath("/image");
        File file = new File(realPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        try {
            String newFileName = UUID.randomUUID().toString();
            String originalFilename = filename.getOriginalFilename();
            String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
            filename.transferTo(new File(file.toString() + "/" + newFileName + substring));

            account.setImage(newFileName + substring);
        } catch (Exception e) {
            account.setImage(null);
        }
        String acc = ((Account) session.getAttribute("user")).getAccount();
        account.setAccount(acc);
        int i = accountService.update(account);

        session.removeAttribute("user");
        Account account1 = accountService.loadAccount(acc);
        session.setAttribute("user", account1);

        return "/account/AccountXX.html";
    }

    @RequestMapping("AccountFile")
    public String AccountFile(MultipartFile filename, HttpSession session) throws IOException {
        File file = new File("C:\\Users\\a\\Desktop\\S3_bootstrap项目\\Demo\\bootstrop_front-end\\src\\main\\webapp/简历");
        if (!file.exists()) {
            file.mkdirs();
        }
        Account account = (Account) session.getAttribute("user");
        String newFileName = UUID.randomUUID().toString();
        String originalFilename = filename.getOriginalFilename();
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        filename.transferTo(new File(file.toString() + "/" + newFileName + substring));

        account.setRdocument(newFileName + substring);

        account.setAccount(account.getAccount());
        int i = accountService.update(account);

        session.removeAttribute("user");
        Account account1 = accountService.loadAccount(account.getAccount());
        session.setAttribute("user", account1);

        return "/account/AccountFile.html";
    }

    // --------------------------

    @RequestMapping("Page")
    @ResponseBody
    public PageVo<Account> Page(@RequestBody PageVo<Account> pageVo){
        return accountService.Page(pageVo.getObj(),pageVo.getNum(),pageVo.getSize());
    }

    @RequestMapping("Register")
    @ResponseBody
    public int Register(String account){
        Account acc = accountService.loadAccount(account);
        return acc==null?1:-1;
    }

    @RequestMapping("Add")
    @OpLog(opModule = "用户模块",operType = "添加用户",operDesc = "此方法用于添加用户")
    public String Add(Account account, HttpServletRequest req, MultipartFile filename){
        String realPath = req.getServletContext().getRealPath("/image");
        File file = new File(realPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        try {
            String newFileName = UUID.randomUUID().toString();
            String originalFilename = filename.getOriginalFilename();
            String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
            filename.transferTo(new File(file.toString() + "/" + newFileName + substring));

            account.setImage(newFileName + substring);
        } catch (Exception e) {
            account.setImage(null);
        }
        account.setAccount(account.getAccount());
        account.setAccount(account.getAccount());
        int i = accountService.insert(account);
        return "/account/Account.html";
    }

    @RequestMapping("UpDate")
    @OpLog(opModule = "用户模块",operType = "修改用户",operDesc = "此方法用于修改用户")
    public String UpDate(Account account, HttpServletRequest req, MultipartFile filename){
        String realPath = req.getServletContext().getRealPath("/image");
        File file = new File(realPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        try {
            String newFileName = UUID.randomUUID().toString();
            String originalFilename = filename.getOriginalFilename();
            String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
            filename.transferTo(new File(file.toString() + "/" + newFileName + substring));

            account.setImage(newFileName + substring);
        } catch (Exception e) {
            account.setImage(null);
        }
        account.setAccount(account.getAccount());
        int i = accountService.update(account);
        return "/account/Account.html";
    }

    @RequestMapping("Delete")
    @OpLog(opModule = "用户模块",operType = "删除用户",operDesc = "此方法用于删除用户")
    public String Delete(String account){
        Accountrole accountrole = accountroleService.AccountroleOnt(1);
        if (accountrole.getAccid().equals(account)){
            return "/Butongguo.html";
        }else {
            int i = accountService.delete(account);
            return "/account/Account.html";
        }
    }

//    /**
//     * 把用户名转成拼音
//     */
    @RequestMapping("changeChineseToPinyin")
    @ResponseBody
    public Map<String,Object> changeChineseToPinyin(String username){
        Map<String,Object> map=new HashMap<>();
        if(null!=username) {
            String pingYin = PinyinUtils.getPingYin(username);
            map.put("value", pingYin);
        }else {
            map.put("value", "");
        }
        return map;
    }





}
